package netConnection
{
	import flash.display.Sprite;
	import flash.events.AsyncErrorEvent;
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import flash.events.SecurityErrorEvent;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import flash.net.NetStreamAppendBytesAction;
	import flash.net.NetStreamInfo;
	import flash.net.ObjectEncoding;
	import flash.net.Responder;
	
	public class NetConnectionExample extends Sprite {
		//private var videoURL:String = "http://www.helpexamples.com/flash/video/cuepoints.flv";
		private var rtmpService:String = "rtmp://localhost/vod/";
		private var videoName:String = "sample";
		private var connection:NetConnection;
		private var stream:NetStream;
		private var video:Video = new Video();        
		
		public function NetConnectionExample() {
			connection = new NetConnection();
			//connection.objectEncoding = ObjectEncoding.AMF3;
			connection.client = {};
			connection.client.onBWCheck = function onBWCheck():void{};
			connection.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			connection.connect(rtmpService);
		}
		
		private function netStatusHandler(event:NetStatusEvent):void {
			trace("---------------------------------------------" + event.info.code);
			switch (event.info.code) {
				case "NetConnection.Connect.Success":
					connectStream();
					break;
				case "NetStream.Play.StreamNotFound":
					trace("Stream not found: " + rtmpService + videoName);
					break;
			}
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function connectStream():void {
			var stream:NetStream = new NetStream(connection);
			var responder:Responder = new Responder(responderHander, statusHandler);
			connection.call("", responder);
			stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
			stream.client = new CustomClient();
			video.attachNetStream(stream);
			stream.play(videoName);
			addChild(video);
			var streamInfo:NetStreamInfo = stream.info;
			
			trace(streamInfo.SRTT);
			trace(streamInfo.videoByteCount);
			trace(streamInfo.droppedFrames);
			trace(streamInfo);
		}
		private function responderHander(o:*):void
		{
			trace(o);
		}
		private function statusHandler(o:*):void
		{
			trace(o);
		}
	}
}

class CustomClient {
	public function onMetaData(info:Object):void {
		trace("metadata: duration=" + info.duration + " width=" + info.width + " height=" + info.height + " framerate=" + info.framerate);
	}
	public function onCuePoint(info:Object):void {
		trace("cuepoint: time=" + info.time + " name=" + info.name + " type=" + info.type);
	}
	public function onXMPData(info:Object):void {
		
	}
}